package com.hopu.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Sort;
import com.hopu.service.SortService;
import com.hopu.util.ResultUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 房屋类别相关接口
 */
@RestController
@RequestMapping("sort")
public class SortController {
    @Resource
    private SortService sortServiceImpl;

    /**
     * 查看所有房屋类别（带分页）
     * @param current 当前页
     * @param pageSize 页大小（每页显示个数）
     * @return
     */
    @PostMapping("/findAllSort")
    private ResultUtils findAllSort(
            @RequestParam(name = "current",defaultValue = "1",required = true) Integer current,
            @RequestParam(name = "pageSize",defaultValue = "14")Integer pageSize
    ){
        PageHelper.startPage(current,pageSize);
        List<Sort> allSort = sortServiceImpl.findAllSort();
        PageInfo<Sort> pageInfo = new PageInfo<>(allSort);

        return new ResultUtils(200,"SUCCESS",pageInfo);
    }

    /**
     * 查看是否存在
     * @param sname
     * @param sid
     * @return
     */
    @PostMapping("/findSortByNameOrId")
    private ResultUtils findSortByNameOrId (@RequestParam(name ="sname") String sname,
                                            @RequestParam(name = "sid") Integer sid) {
        Sort isSort = sortServiceImpl.findSortByNameOrId(sname, sid);
        if (!ObjectUtils.isEmpty(isSort)) {
            return new ResultUtils(200,"SUCCESS",isSort);
        }
        return new ResultUtils(400,"ERROR",isSort);

    }

    /**
     * 添加
     * @param sname
     * @return
     */
    @PostMapping("/addSort")
    private ResultUtils addSort (@RequestParam(name ="sname",required = true) String sname){
        boolean isruset = sortServiceImpl.addSort(sname);
        if (isruset){
            return new ResultUtils(200,"SUCCESS",null);
        }
        return new ResultUtils(400,"ERROR",null);
    }

    /**
     * 修改
     * @param sid
     * @param sname
     * @return
     */
    @PostMapping("/uppSort")
    private ResultUtils uppSort (
            @RequestParam(name = "sid",required = true) Integer sid,
            @RequestParam(name = "sname",required = true) String sname
    ) {
        System.out.println("===="+sname+"========="+sid);
        boolean uppSort = sortServiceImpl.uppSort(sid, sname);
        if (uppSort) {
            return new ResultUtils(200,"SUCCESS",null);
        }
        return new ResultUtils(400,"ERROR",null);
    }

    /**
     * 查询下拉框的房屋类型
     * @return
     */
    @PostMapping("findAllName")
    private ResultUtils findAllName(){
       List<Sort> list = sortServiceImpl.findAllName();
       return new ResultUtils(200,"SUCCESS",list);
    }
}
